package com.gensdai.leliang.json_base;

import android.text.TextUtils;
import android.util.Log;

import com.gensdai.leliang.entity.Address;
import com.gensdai.leliang.entity.Call;
import com.gensdai.leliang.entity.CardCart;
import com.gensdai.leliang.entity.ShopBean;
import com.gensdai.leliang.entity.User;
import com.gensdai.leliang.entity.UserNameBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/27.
 */

public class json_base {

    /**
     * 简单返回的解析
     */
    public boolean GOOD(String object) {
        boolean yanzhengma_b = true;
        try {
            if (null != object) {
                JSONObject jsonObject = new JSONObject(object);
                if (jsonObject.has("success")) {
                    yanzhengma_b = jsonObject.getBoolean("success");
                }

            } else {
                System.out.println("什么也没有");
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("解析出现错误");
        }
        System.out.println("成功");
        return yanzhengma_b;
    }

    /**
     * 解析登录
     *
     * @return
     */
    public Object jsonEnter(JSONObject object) {
        UserNameBean user = null;
        Address address;
        try {
            JSONObject obj = object.getJSONObject("data");
            address = new Address();
            user = new UserNameBean();
            user.setUid(obj.getString("userId"));
            user.setHasPayPassword(obj.getBoolean("hasPayPassword"));
            user.setDaifaNum(obj.getString("daifaNum"));
            user.setDaishouNum(obj.getString("daishouNum"));
            user.setDaifuNum(obj.getString("daifuNum"));
            user.setWaitComment(obj.getString("waitComment") + "");
            user.setMoney(obj.getDouble("money") + "");
            user.setMemberType(obj.getString("memberType") + "");
            user.setLevel(obj.optString("level"));
            user.setToken(obj.optString("token") + "");
            user.setPhone(obj.getString("phone") + "");
            user.setUserno(obj.getString("userNo") + "");
            user.setInviteNumber(obj.getString("recommendUserNo") + "");
            if (obj.getString("nickName").equals("null") || obj.getString("nickName") == null) {
                user.setName("");
            } else {
                user.setName(obj.getString("nickName") + "");
            }
            address.setPhone(obj.getJSONObject("defaultAddress").getString("adressPhone"));
            address.setUser_name(obj.getJSONObject("defaultAddress").getString("name"));
            address.setCityId(obj.getJSONObject("defaultAddress").getString("province"));
            address.setId(obj.getJSONObject("defaultAddress").getString("addressId"));
            address.setAddress(obj.getJSONObject("defaultAddress").getString("detailAddress"));

//            if(obj.has("singleIPostPrice")){
//                address.setSingleIPostPrice(obj.getString("singleIPostPrice"));
//            }
//            if(obj.has("postageTip")){
//                address.setPostageTip(obj.getString("postageTip"));
//            }
//            if(obj.has("fullPostPrice")){
//                address.setFullPostPrice(Integer.parseInt(obj.getString("fullPostPrice")));
//            }
            user.setAddress(address);

            if (!obj.isNull("appModule")) {
                JSONObject oj = obj.getJSONObject("appModule");
                UserNameBean.appModule mo = new UserNameBean.appModule();
                mo.myWallet = oj.optString("myWallet");
                mo.bindAlipay = oj.optString("bindAlipay");
                mo.contactUs = oj.optString("contactUs");
                mo.receiveAddress = oj.optString("receiveAddress");
                mo.changePassword = oj.optString("changePassword");
                mo.inviteFriends = oj.optString("inviteFriends");
                mo.myCollection = oj.optString("myCollection");
                user.setMappModule(mo);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return user;
    }

    /**
     * 解析用户资金
     *
     * @return
     */
    public Object jsonWallet(JSONObject object) {
        UserNameBean user = null;
        try {
            JSONObject obj = object.getJSONObject("data");
            user = new UserNameBean();
            user.setGrouthJin(obj.getJSONObject("amountMap").optString("grouthJinAccount"));
            user.setGrouthJinBlance(obj.getJSONObject("amountMap").optString("grouthJinBlance"));
            user.setTotalAmount(obj.getJSONObject("amountMap").optString("totalAccount"));
            user.setGrouthBei(obj.getJSONObject("amountMap").optString("grouthBeiAccount"));
            user.setGrouthBeiBlance(obj.getJSONObject("amountMap").optString("grouthBeiBlance"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return user;
    }


    /**
     * 解析所有收货地址
     *
     * @param object
     * @return
     */
    public Object jsonDelivery(JSONObject object) {
        Address address;
        List<Address> list = null;
        try {
            JSONArray array = object.getJSONObject("data").getJSONArray("addressList");
            list = new ArrayList<Address>();
            for (int i = 0; i < array.length(); i++) {
                address = new Address();
                JSONObject obj = array.getJSONObject(i);
                address.setUser_name(obj.getString("name"));
                address.setAddress(obj.getString("address"));
                address.setPhone(obj.getString("phone"));
                address.setDefault(obj.getInt("ifDefault") + "");
                address.setId(obj.getString("id"));
                address.setCityId(obj.getString("cityId"));
                address.setProvinceId(obj.optString("provinceId"));
                address.setDistrictId(obj.optString("districtId"));
                list.add(address);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return list;
    }

    /**
     * 解析订单
     *
     * @param object
     * @return
     */
    public Object jsonIndent(JSONObject object) {
        ShopBean shopBean;
        CardCart cardCart;
        List<CardCart> list = null;
        List<ShopBean> mlist = null;
        try {
            JSONArray array = object.getJSONArray("data");

            mlist = new ArrayList<ShopBean>();
            for (int i = 0; i < array.length(); i++) {
                list = new ArrayList<CardCart>();
                shopBean = new ShopBean();
                JSONArray arry = array.getJSONArray(i);
                for (int num = 0; num < arry.length() - 1; num++) {
                    cardCart = new CardCart();
                    cardCart.setShopname(arry.getJSONObject(num).getString("proName"));
                    cardCart.setProductAttributeName(arry.getJSONObject(num).getString("attributeName"));
                    cardCart.setProductAttributeId(arry.getJSONObject(num).getString("productAttributeId"));
                    cardCart.setOrderAttributeId(arry.getJSONObject(num).getString("orderAttributeId"));
                    cardCart.setPrice(arry.getJSONObject(num).getString("buyPrice"));
                    cardCart.setNumber(arry.getJSONObject(num).getString("buyNum"));
                    cardCart.setIc_path(arry.getJSONObject(num).getString("logo"));
                    list.add(cardCart);
                }
                shopBean.setIndentAllNumber(arry.getJSONObject(arry.length() - 1).getString("totalNum") + "");
                shopBean.setAll_money(arry.getJSONObject(arry.length() - 1).getString("orderPrice") + "");
                shopBean.setFreight(arry.getJSONObject(arry.length() - 1).getInt("transFerFee") + "");
                shopBean.setIndentId(arry.getJSONObject(arry.length() - 1).getString("orderId"));
                shopBean.setCardCartsList(list);
                shopBean.setIndent_state(arry.getJSONObject(arry.length() - 1).getInt("orderState") + "");
                shopBean.setWaitPayPrice(arry.getJSONObject(arry.length() - 1).getDouble("waitPayPrice"));
                shopBean.setHasEvaluate(arry.getJSONObject(arry.length() - 1).getString("hasEvaluate"));
                if (arry.getJSONObject(arry.length() - 1).has("type")) {
                    shopBean.setType(arry.getJSONObject(arry.length() - 1).getString("type"));
                }
                shopBean.setInfo(arry.getJSONObject(arry.length() - 1).getString("info"));
                shopBean.setIfFailure(arry.getJSONObject(arry.length() - 1).getString("ifFailure"));
                mlist.add(shopBean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return mlist;
    }

    /**
     * 解析购物车订单
     *
     * @param object
     * @return
     */
    public Object jsonCartCard(JSONObject object) {
        CardCart cardCart;
        List<CardCart> list = null;
        try {
            JSONArray array = object.getJSONArray("shopCarProdList");
            list = new ArrayList<CardCart>();
            for (int i = 0; i < array.length(); i++) {
                cardCart = new CardCart();
                JSONObject obj = array.getJSONObject(i);
                cardCart.setShopname(obj.getString("productName"));
                cardCart.setPrice(obj.getDouble("productPrice") + "");
                cardCart.setNumber(obj.getInt("productNum") + "");
                cardCart.setProductId(obj.getString("productId"));
                cardCart.setIc_path(obj.getString("productLogo"));
                cardCart.setProductAttributeName(obj.getString("productAttributeName"));
                cardCart.setId(obj.getString("id"));
                cardCart.setProductAttributeId(obj.getString("productAttributeId"));
                cardCart.setSurplusNumber(obj.getString("productStockTips"));
                cardCart.setSelect_state("0");
                cardCart.setShopCarState(obj.getInt("shopCarState") + "");
                list.add(cardCart);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return list;
    }

    /**
     * 解析成长贝记录
     *
     * @param object
     * @return
     */
    public Object JsonGetGrithBei(JSONObject object) {
        User user;
        List<User> list = null;
        try {
            JSONArray array = object.getJSONArray("data");
            list = new ArrayList<User>();
            for (int i = 0; i < array.length(); i++) {
                user = new User();
                JSONObject obj = array.getJSONObject(i);
                user.setIndentMoney(obj.getString("orderAmount"));
                user.setGiveMoneyTime(obj.getString("createTime"));
                user.setReachState(obj.getString("status"));
                user.setGetCZBTime(obj.getString("amount"));
                user.setReachTime(obj.getString("effectTime"));
                list.add(user);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 解析成长金记录
     *
     * @param object
     * @return
     */
    public Object JsonGetGrithJei(JSONObject object) {
        User user;
        List<User> list = null;
        try {
            JSONArray array = object.getJSONArray("data");
            list = new ArrayList<User>();
            for (int i = 0; i < array.length(); i++) {
                user = new User();
                JSONObject obj = array.getJSONObject(i);
                user.setRank(obj.getString("recomLevel") + "");
                user.setIndentMoney(obj.getString("orderAccount") + "");
                user.setGetCZJmmoney(obj.getString("reward") + "");
                user.setNickName(obj.getString("nickname"));
                user.setGiveMoneyTime(obj.getString("addTime"));
                user.setReachState(obj.getString("status"));
                list.add(user);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 解析成长贝发放记录
     *
     * @param object
     * @return
     */
    public Object JsonGrantBei(JSONObject object) {
        User user;
        List<User> list = null;
        try {
            JSONArray array = object.getJSONArray("data");
            list = new ArrayList<User>();
            for (int i = 0; i < array.length(); i++) {
                user = new User();
                JSONObject obj = array.getJSONObject(i);
                user.setGrantMoney(obj.getDouble("amount") + "");
                user.setGrantTime(obj.getString("effectTime") + "");
                list.add(user);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 解析成长贝使用记录
     *
     * @param object
     * @return
     */
    public Object JsonUseBei(JSONObject object) {
        User user;
        List<User> list = null;
        try {
            JSONArray array = object.getJSONArray("data");
            list = new ArrayList<User>();
            for (int i = 0; i < array.length(); i++) {
                user = new User();
                JSONObject obj = array.getJSONObject(i);
                user.setIndentMoney(obj.getDouble("amount") + "");
                user.setGiveMoneyTime(obj.getString("createTime") + "");
                user.setUseCZB(obj.getDouble("shellamount") + "");
                user.setTrueMoney(obj.getDouble("payamount") + "");
                list.add(user);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 解析系统消息
     *
     * @param object
     * @return
     */
    public Object JsonSystemCall(JSONObject object) {
        Call call;
        List<Call> list = null;
        try {
            if (object.getString("data") == null) {
                return null;
            }
            JSONArray array = object.getJSONArray("data");
            list = new ArrayList<Call>();
            for (int i = 0; i < array.length(); i++) {
                call = new Call();
                JSONObject obj = array.getJSONObject(i);
                call.setTime(obj.getString("send_datetime") + "");
                call.setDetails(obj.getString("msg_content"));
                call.setTitle(obj.getString("msg_title"));
                call.setId(obj.getString("id") + "");
                call.setCall_state(obj.getString("msg_status") + "");
                call.setType(obj.getString("msg_flag") + "");
                list.add(call);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 解析订单消息
     *
     * @param object
     * @return
     */
    public Object JsonIndentCall(JSONObject object) {
        Call call;
        List<Call> list = null;
        try {
            if (TextUtils.isEmpty(object.getString("data"))) {
                return null;
            }
            JSONArray array = object.getJSONArray("data");
            list = new ArrayList<Call>();
            for (int i = 0; i < array.length(); i++) {
                call = new Call();
                JSONObject obj = array.getJSONObject(i);
                call.setTime(obj.getString("sendDatetime") + "");
                call.setState(obj.getString("msgContent") + "");
                call.setId(obj.getString("id") + "");
                call.setLogisticsCompany(obj.getString("logisticsCompany") + "");
                call.setProductName(obj.getString("productName") + "");
                call.setWayBillNo(obj.getString("wayBillNo") + "");
                call.setProductPic(obj.getString("productPic") + "");
                call.setCall_state(obj.getString("msg_status") + "");
                call.setOrderID(obj.getString("orderId") + "");

                list.add(call);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 解析好友邀请记录
     *
     * @param object
     * @return
     */
    public Object JsonInviteDeal(JSONObject object) {
        User user;
        List<User> list = null;
        try {
            if (TextUtils.isEmpty(object.getString("data"))) {
                return null;
            }
            JSONArray array = object.getJSONArray("data");
            list = new ArrayList<User>();
            for (int i = 0; i < array.length(); i++) {
                user = new User();
                JSONObject obj = array.getJSONObject(i);
                user.setNickName(obj.getString("nickname"));
                user.setRecommendNumber(obj.getString("recommendNumber"));
                user.setRewardAmount(obj.getString("rewardAmount"));
                user.setRewardRatio(obj.getString("rewardRatio"));
                user.setTotalConsumption(obj.getString("totalConsumption"));
                list.add(user);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }


    /**
     * 解析消息
     *
     * @param object
     * @return
     */
    public Object JsonCall(JSONObject object) {
        String[] list = null;
        try {
            if (TextUtils.isEmpty(object.getString("data"))) {
                return null;
            }
            list = new String[2];
            JSONObject obj = object.getJSONObject("data");
            list[0] = obj.getString("systemMsgCount");
            list[1] = obj.getString("orderMsgCount");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 解析个人信息
     *
     * @param object
     * @return
     */
    public Object JsonUserInfo(JSONObject object) {
        User user = null;
        try {
            if (TextUtils.isEmpty(object.getString("data"))) {
                return null;
            }
            user = new User();
            JSONObject obj = object.getJSONObject("data");
            user.setHandpic(obj.getString("handpic"));
            user.setSex(obj.getString("sex"));
            user.setEmail(obj.getString("email"));
            user.setNickName(obj.getString("nickname"));
            user.setProvince(obj.getString("province"));
            user.setDistrict(obj.getString("district"));
            if (obj.has("userPregnantState")) {
                user.setUserPregnantState(obj.getString("userPregnantState"));
            }
            user.setCity(obj.getString("city"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return user;
    }

    /**
     * 解析孕育信息
     *
     * @param object
     * @return
     */
    public Object JsonUserBBInfo(JSONObject object) {
        User user = null;
        try {
            if (TextUtils.isEmpty(object.getString("data"))) {
                return null;
            }
            JSONObject obj = object.getJSONObject("data");
            user = new User();
            user.setBabyExceptedBirthday(obj.getString("babyExceptedBirthday"));
            user.setBabyBirthday(obj.getString("babyBirthday"));
            user.setBabyNickname(obj.getString("babyNickname"));
            user.setBabyBirthWeight(obj.getString("babyBirthWeight"));
            user.setBabySex(obj.getString("babySex"));
            user.setBabyBirthHeight(obj.getString("babyBirthHeight"));
            if (obj.has("userPregnantState")) {
                user.setUserPregnantState(obj.getString("userPregnantState"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return user;
    }

    /**
     * 解析第三方绑定状态
     *
     * @param object
     * @return
     */
    public Object JsonDSF(JSONObject object) {
        UserNameBean user = null;
        try {
            JSONObject obj = object.getJSONObject("data");
            user = new UserNameBean();
            user.setIfBindQQ(obj.getString("ifBindQQ"));
            user.setIfBindWeChat(obj.getString("ifBindWeChat"));

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return user;
    }

}
